class AdminController < ApplicationController
  require_role 'admin'
  def index
    @title = "Administration Page"
    flash[:notice] = ""

    if params[:save]

      if request.post?
        # checked   => update "T" (Exclude user)
        # unchecked => update "F" (Not exclude user)
        User.update_all(["exclude = :exclude", { :exclude => "T" } ], :id => params[:excUser_ids])
        if params[:excUser_ids]
          User.update_all(["exclude = :exclude", { :exclude => "F" } ], ["(id NOT IN (:excIDList)) AND (id IN (:hidIDList))", { :excIDList => params[:excUser_ids], :hidIDList => params[:hidUser_ids] } ])
        else
          # Case all records are unchecked (params[:excUser_ids] is nil) : UPDATE all records on screen by exclude = "F"
          User.update_all(["exclude = :exclude", { :exclude => "F" } ], ["id IN (:hidIDList)", { :hidIDList => params[:hidUser_ids] } ])
        end

        if !params[:hidChkUser].nil?
          if params[:hidChkUser].length == 0 then
            params[:hidChkUser] = nil
          end
        end

        if !params[:hidChkLogin].nil?
          if params[:hidChkLogin].length == 0 then
            params[:hidChkLogin] = nil
          end
        end

        if !params[:hidChkEmail].nil?
          if params[:hidChkEmail].length == 0 then
            params[:hidChkEmail] = nil
          end
        end

        redirect_to :action => "index", :sUser => params[:hidSearch], :chkUser => params[:hidChkUser], :chkLogin => params[:hidChkLogin], :chkEMail => params[:hidChkEmail]
      end

    else

      if params[:sUser] && params[:sUser].length > 0
        # Case search

        if params[:chkUser] || params[:chkLogin] || params[:chkEMail]
          # Search condition
          @sCaption = ""
          @sCond = ""
          if params[:chkUser]
            @sCaption = params[:chkUser] + " & "
            @sCond = "(UPPER(u.name) like UPPER(:condUser))" + " OR "
          end

          if params[:chkLogin]
            @sCaption = @sCaption + params[:chkLogin] + " & "
            @sCond = @sCond + "(UPPER(u.login) like UPPER(:condUser))" + " OR "
          end

          if params[:chkEMail]
            @sCaption = @sCaption + params[:chkEMail] + " & "
            @sCond = @sCond + "(UPPER(u.email) like UPPER(:condUser))" + " OR "
          end

          @sUser = params[:sUser].strip
          @users = User.find(:all, :joins => "as u inner join roles_users ru on u.id = ru.user_id inner join roles r on ru.role_id = r.id",
                             :select => "u.id, u.login, u.email, u.name AS user_name, u.created_at, u.activated_at, u.state, u.exclude, u.deleted_at, r.name",
                             :conditions => [ @sCond[0,@sCond.length - 4], { :condUser => @sUser + "%" } ],
                             :order => "u.created_at DESC")
        else
          flash[:notice] = "Please select search condition."
          @users = User.find(:all, :conditions => "login IS NULL")
        end
      else
        @users = User.find(:all, :joins => "as u inner join roles_users ru on u.id = ru.user_id inner join roles r on ru.role_id = r.id",
                           :select => "u.id, u.login, u.email, u.name AS user_name, u.created_at, u.activated_at, u.state, u.exclude, u.deleted_at, r.name",
                           :order => "u.created_at DESC")
      end

    end
  end

  def histStat
    @title = "Administration Page"
    @users = User.find(:all, :select => "TO_CHAR(created_at, 'YYYY-MM-DD') AS grpdate, COUNT(id) AS totaluser",
                       :group => "TO_CHAR(created_at, 'YYYY-MM-DD')",
                       :order => "TO_CHAR(created_at, 'YYYY-MM-DD') DESC")
  end


end
